﻿namespace Pub.Class
{
    using System;
    using System.Data;

    public class ExcelWriter : Disposable
    {
        private readonly IExcelWriter excelWriter;

        public ExcelWriter(string excelPath)
        {
            if (this.excelWriter.IsNull())
            {
                this.excelWriter = (IExcelWriter) (WebConfig.GetApp("ExcelWriterProviderName") ?? "Pub.Class.Excel.OleDb.ExcelWriter,Pub.Class.Excel.OleDb").LoadClass();
                this.excelWriter.Open(excelPath);
            }
        }

        public ExcelWriter(string classNameAndAssembly, string excelPath)
        {
            if (this.excelWriter.IsNull())
            {
                this.excelWriter = (IExcelWriter) classNameAndAssembly.IfNullOrEmpty("Pub.Class.Excel.OleDb.ExcelWriter,Pub.Class.Excel.OleDb").LoadClass();
                this.excelWriter.Open(excelPath);
            }
        }

        public ExcelWriter(string dllFileName, string className, string excelPath)
        {
            if (this.excelWriter.IsNull())
            {
                this.excelWriter = (IExcelWriter) dllFileName.LoadClass(className);
                this.excelWriter.Open(excelPath);
            }
        }

        public ExcelWriter Delete(string tableName)
        {
            this.excelWriter.Delete(tableName);
            return this;
        }

        protected override void InternalDispose()
        {
            this.excelWriter.Dispose();
            base.InternalDispose();
        }

        public ExcelWriter ToExcel(DataSet ds)
        {
            this.excelWriter.ToExcel(ds);
            return this;
        }

        public ExcelWriter ToExcel(DataTable dt)
        {
            this.excelWriter.ToExcel(dt);
            return this;
        }
    }
}

